package pl.nazaweb.samplesandtutorials;

import pl.nazaweb.samplesandtutorials.abstracts.ShopList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;

public class ShopProviderActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_provider_test);
		testShopProvider();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.shop_provider_test, menu);
		return true;
	}

	private void testShopProvider() {
		String tag = "testowanie dosatwcy listy zakupow";
		Log.d(tag, "dodanie pozycji do listy");
		ShopList shopList = new ShopList();
		for (int x = 0; x < 10; x++) {
			ContentValues values = new ContentValues();
			values.put("name", "item" + x);
			values.put("price", x * 1.56);
			ContentResolver contentResolver = getContentResolver();
			Uri uri = shopList.getContentUri();
			Log.d(tag, "wstawiany uri : " + uri);
			Uri insertedUri = contentResolver.insert(uri, values);
			Log.d(tag, "wstawiony uri : " + insertedUri);
		}
		Log.d(tag, "pobranie rekordu o numerze 7");
		Uri uriRow10 = Uri.withAppendedPath(shopList.getContentUri(), "7");
		Cursor cur = managedQuery(uriRow10, null, null, null, null);
		if (cur.moveToFirst()) {
			Log.d(tag,
					"id : " + cur.getString(1) + ", nazwa : "
							+ cur.getString(cur.getColumnIndex("name"))
							+ " cena : "
							+ cur.getDouble(cur.getColumnIndex("price")));

		}
		cur.close();
		Uri allShopList = shopList.getContentUri();
		Cursor c = managedQuery(allShopList, null, null, null, null);
		Log.d(tag, "wszystkich itemów w liscie : " + c.getCount());
	}
}
